﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVCClubSite.IDAL;
using MVCClubSite.Model;

namespace MVCClubSite.LinqToSQLDAL
{
    /// <summary>
    /// Linq To SQL DAL for locations
    /// </summary>
    public class Locations : ILocations
    {
        DB _db;

        /// <summary>
        /// Default constructor
        /// </summary>
        public Locations()
        {
            _db = new DB();
        }

        /// <summary>
        /// Constructor used to pass in a datacontext
        /// </summary>
        /// <param name="db">datacontext for the DAL to use</param>
        public Locations(DB db)
        {
            _db = db;
            if (_db == null)
                throw new InvalidOperationException("DB cannot be null.");
        }

        public IQueryable<Location> GetLocations()
        {
            return from l in _db.cs_Locations
                   select new Location
                   {
                       ID = l.LocationID,
                       Title = l.Title,
                       Description = l.Description,
                       Url = l.Url,
                       Address = l.Address,
                       ImageID = l.ImageID
                   };
        }

        public Location GetLocationByID(int id)
        {
            return (from l in _db.cs_Locations
                    where l.LocationID == id
                    select new Location
                    {
                        ID = l.LocationID,
                       Title = l.Title,
                       Description = l.Description,
                       Url = l.Url,
                       Address = l.Address,
                       ImageID = l.ImageID
                    }).SingleOrDefault();
        }

        public void Save(Location location)
        {
            using (DB db = new DB())
            {
                cs_Location dbLocation = db.cs_Locations.Where(l => l.LocationID == location.ID).SingleOrDefault();
                bool isNew = false;
                if (dbLocation == null)
                {
                    dbLocation = new cs_Location();
                    isNew = true;
                }

                dbLocation.Title = location.Title;
                dbLocation.Description = location.Description;
                dbLocation.Url = location.Url;
                dbLocation.Address = location.Address;
                dbLocation.ImageID = location.ImageID;

                if (isNew)
                    db.cs_Locations.InsertOnSubmit(dbLocation);

                db.SubmitChanges();
            }
        }

        public void Delete(int id)
        {
            cs_Location dbLocation = _db.cs_Locations.Where(l => l.LocationID == id).SingleOrDefault();

            if (dbLocation != null)
            {
                _db.cs_Locations.DeleteOnSubmit(dbLocation);
                _db.SubmitChanges();
            }
        }
    }
}
